/*
 * Niiuzu
 * Copyright (C) 2011  Thierry Nowak
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.niiuzu.server.rpc.impl;

import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.niiuzu.server.util.IdBuilder;
import com.niiuzu.shared.exception.ServiceRpcException;
import com.niiuzu.shared.model.FeedChannel;
import com.niiuzu.shared.model.FeedChannelGroup;
import com.niiuzu.shared.model.FeedStyle;
import com.niiuzu.shared.model.UserSettings;
import com.niiuzu.shared.rpc.UserService;

/**
 * Implémentation du service dédié aux utilisateurs.
 */
public class UserServiceImpl extends RemoteServiceServlet implements
		UserService {

	private static final long serialVersionUID = 3433845667124813710L;

	private static final Logger LOGGER = Logger.getLogger(UserServiceImpl.class
			.getName());

	private static FeedChannelGroup[] initialChannelGroups;
	private static FeedChannel[] initialChannels;

	static {
		// Initialisation des thèmes et flux affichés à démarrage
		initialChannelGroups = new FeedChannelGroup[3];
		initialChannels = new FeedChannel[7];

		IdBuilder idBuilder = IdBuilder.getInstance();

		FeedChannelGroup channelGroup1, channelGroup2, channelGroup3;
		FeedChannel channel;

		int index = 0;

		channelGroup1 = new FeedChannelGroup();
		channelGroup1.setId(idBuilder.getNewId(FeedChannelGroup.class));
		channelGroup1.setName("Actualites");
		channelGroup1.setStyle(FeedStyle.BLUE);
		initialChannelGroups[index++] = channelGroup1;

		channelGroup2 = new FeedChannelGroup();
		channelGroup2.setId(idBuilder.getNewId(FeedChannelGroup.class));
		channelGroup2.setName("Sport");
		channelGroup2.setStyle(FeedStyle.GREEN);
		initialChannelGroups[index++] = channelGroup2;

		channelGroup3 = new FeedChannelGroup();
		channelGroup3.setId(idBuilder.getNewId(FeedChannelGroup.class));
		channelGroup3.setName("Informatique");
		channelGroup3.setStyle(FeedStyle.RED);
		initialChannelGroups[index++] = channelGroup3;

		index = 0;

		// Le Monde
		channel = new FeedChannel();
		channel.setId(idBuilder.getNewId(FeedChannel.class));
		channel.setName("Le Monde");
		channel.setLink("http://www.lemonde.fr/rss/une.xml");
		channel.setChannelGroupId(channelGroup1.getId());
		initialChannels[index++] = channel;

		// Google News
		channel = new FeedChannel();
		channel.setId(idBuilder.getNewId(FeedChannel.class));
		channel.setName("Google News");
		channel
				.setLink("http://news.google.com/news?pz=1&cf=all&ned=fr&hl=fr&output=rss");
		channel.setChannelGroupId(channelGroup1.getId());
		initialChannels[index++] = channel;

		// L'Equipe - Foot
		channel = new FeedChannel();
		channel.setId(idBuilder.getNewId(FeedChannel.class));
		channel.setName("L'Equipe (Foot)");
		channel
				.setLink("http://www.lequipe.fr/Xml/Football/Titres/actu_rss.xml");
		channel.setChannelGroupId(channelGroup2.getId());
		initialChannels[index++] = channel;

		// Google News - Tennis
		channel = new FeedChannel();
		channel.setId(idBuilder.getNewId(FeedChannel.class));
		channel.setName("Google News (Tennis)");
		channel
				.setLink("http://news.google.com/news?ned=fr&output=rss&q=tennis");
		channel.setChannelGroupId(channelGroup2.getId());
		initialChannels[index++] = channel;

		// Google News - Handball
		channel = new FeedChannel();
		channel.setId(idBuilder.getNewId(FeedChannel.class));
		channel.setName("Google News (Handball)");
		channel
				.setLink("http://news.google.com/news?ned=fr&output=rss&q=handball");
		channel.setChannelGroupId(channelGroup2.getId());
		initialChannels[index++] = channel;

		// Clubic
		channel = new FeedChannel();
		channel.setId(idBuilder.getNewId(FeedChannel.class));
		channel.setName("Clubic");
		channel.setLink("http://www.clubic.com/articles.rss");
		channel.setChannelGroupId(channelGroup3.getId());
		initialChannels[index++] = channel;

		// IT News
		channel = new FeedChannel();
		channel.setId(idBuilder.getNewId(FeedChannel.class));
		channel.setName("IT News");
		channel.setLink("http://www.itnews.com/feed");
		channel.setChannelGroupId(channelGroup3.getId());
		initialChannels[index++] = channel;
	}

	@Override
	public UserSettings retrieveUserSettings() throws ServiceRpcException {

		LOGGER.info("Récupération des préférences de l'utilisateur");

		UserSettings settings = null;

		try {
			settings = new UserSettings();

			settings.setChannelGroups(initialChannelGroups);
			settings.setChannels(initialChannels);

		} catch (Throwable e) {
			String msg = "Erreur lors de la récupération des préférences";
			LOGGER.log(Level.SEVERE, msg, e);
			throw new ServiceRpcException(msg, e);
		} finally {
			LOGGER.info("Fin de la récupération des préférences");
		}

		return settings;
	}

}
